layui.use(['form','jquery','table','layer'],function() {
    let form = layui.form,
        $ = layui.jquery,
        table =layui.table,
        layer = layui.layer;

    //加载数据表格
    let tabInstance = table.render({
        elem: '#lossList',
        url: '/customer-loss/all', //数据接口
        page: true, //开启分页
        limit: 2, //每页显示的条数，默认10
        limits: [2, 5, 10, 15,], //每页显示条数选择框数据
        // request: {
        //     pageName: 'current',
        //     limitName: 'size'
        // },
        cols: [[ //表头
            {field: 'id', title: '编号', width: 80, sort: true, align:"center"},
            {field: 'customerName', title: '客户',align:"center"},
            {field: 'customerMgr', title: '客户经理',align:"center"},
            {field: 'lastOrderTime', title: '上次下单时间',align:"center"},
            {field: 'lossTime', title: '确认流失时间',align:"center"},
            {field: 'status', title: '状态',templet:"#status",align:"center"},
            {title: '操作', width: 300, fixed: 'right', toolbar: "#lossListBar",align:"center"}
        ]]
    });

    //搜索监听启用
    form.on('submit(search)', function (data) {
        tabInstance.reload({
            where: data.field
        });
        return false;
    });

    function delay(suspend){
        let index = layer.open({
            title :'暂缓流失',
            type : 2,
            content : 'views/khgl/delayLoss.jsp',
            area: ['100%', '100%'],
            btn:['保存', '确定流失', '帮助'],
            btnAlign: 'l',
            success : function(layero, index){
                let body = layer.getChildFrame('body', index);
                if(suspend){
                    body.find(".id").val(suspend.id);
                    body.find(".customerName").val(suspend.customerName);
                    body.find(".customerMgr").val(suspend.customerMgr);
                    body.find(".lastOrderTime").val(suspend.lastOrderTime);
                    body.find(".pauseWay").val(suspend.pauseWay);
                    body.find("#add_pause_way").val(suspend.addPauseWay);
                    form.render();
                }

                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 1
                    });
                },500)
            },

            yes:function(index, layero){
                let delayForm = layer.getChildFrame("body", index);
                let data = {};
                //把数组转为JSON
                delayForm.find("#delay_loss").serializeArray().forEach(function(val){
                    data[val.name] = val.value;
                });
                console.log(data);
                $.post('customer-loss/save-delay',data,function(info){
                    layer.msg(info.msg);
                    tabInstance.reload();
                });
                layer.close(index);
            },
            btn2:function(index, layero){
                determin(suspend);
            }
        })
    }
    $(".help_btn").click(function(){
        let index = layer.open({
            title: '帮助',
            type: 2,
            content: 'views/khgl/help.jsp',
            area: ['600px', '500px'],
            skin:"layui-layer-molv",
            btn: ['确定'],
            btnAlign: 'c',
            yes: function(index, layero){
                layer.close(index);
            }
        })
    });

    function determin(confirm){
        let index = layer.open({
            title :'确认流失',
            type : 2,
            content : 'views/khgl/confirmLoss.jsp',
            area: ['100%', '100%'],
            btn:['保存', '暂缓流失', '帮助'],
            btnAlign: 'l',
            success : function(layero, index){
                let body = layer.getChildFrame('body', index);
                if(confirm){
                    body.find(".id").val(confirm.id);
                    body.find(".customerName").val(confirm.customerName);
                    body.find(".customerMgr").val(confirm.customerMgr);
                    body.find(".lastOrderTime").val(confirm.lastOrderTime);
                    body.find(".pauseWay").val(confirm.pauseWay);
                    body.find("#loss_reason").val(confirm.lossReason);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            },
            yes:function(index, layero) {
                let body = layer.getChildFrame("body", index);
                let data = {};
                //把数组转为JSON
                body.find("#determin_loss").serializeArray().forEach(function (val) {
                    data[val.name] = val.value;
                });
                console.log(data);
                $.post('customer-loss/save-loss', data, function (info) {
                    layer.msg(info.msg);
                    tabInstance.reload();
                });
                layer.close(index);
            },
            btn2:function(index, layero){
                delay(confirm);
            }
        });
    }

    table.on('tool(lossList)', function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值

        if(layEvent === 'suspend') {
            delay(data);
        }else if(layEvent === 'confirm'){
            determin(data);
        }

        // switch (obj.event){
        //     case 'suspend':
        //         //编辑
        //         layer.open({
        //                 title :'暂缓流失',
        //                 type : 2,
        //                 content : 'views/khgl/confirmLoss.jsp',
        //                 btn:['保存', '暂缓流失', '帮助'],
        //                 btnAlign: 'l',
        //                 success : function(layero, index){
        //                     let body = layui.layer.getChildFrame('body', index);
        //
        //                 console.log(obj.data);
        //                 let body = layer.getChildFrame('body',index);
        //                 body.find("#id").val(obj.data.id);
        //                 body.find("#customerName").val(obj.data.customerName);
        //                 body.find("#customerMgr").val(obj.data.customerMgr);
        //                 body.find("#lastOrderTime").val(obj.data.lastOrderTime);
        //                 body.find("#pauseWay").val(obj.data.pauseWay);
        //                 body.find("#loss_reason").val(obj.data.loss_reason);
        //             },
        //             yes: function (index, layero) {
        //                 //user_edit
        //                 let user_form = layer.getChildFrame('body', index).find("#useradd");
        //                 let data = {};
        //                 //把数组转为JSON
        //                 user_form.serializeArray().forEach(function (val) {
        //                     data[val.name] = val.value;
        //                 });
        //                 $.post('customer-loss/save-confirm', data, function (info) {
        //                     layer.close(index);
        //                     layer.msg(info.msg);
        //                     tabInstance.reload();
        //                 });
        //             }
        //         });
        //         break;
        //     case 'del_btn':
        //         layer.confirm('你确定要删除【'+obj.data.dataType+'】吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function(){
        //             $.post('/dataDictionary/del/'+obj.data.id,function(data){
        //                 tabInstance.reload();
        //                 layer.msg(data.msg);
        //             });
        //         });
        // }
    });
})